<?php
class GoogleGeoCode extends Object
{
	public $Address;
	public $LocationData;
	public $Latitude;
	public $Longitude;
	public $Status;
	
	public function loadData()
	{
		if(!self::getItemLoaded('Coords'.self::getAddress()))
		{ 
			if((self::getAddress() != '') && (GOOGLE_API_KEY != ''))
			{ $url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode(self::getAddress()).'&sensor=true&region=no'; }
			$json = file_get_contents($url);
			if($json)
			{ self::setLocationData(json_decode($json)); }
			self::setItemLoaded('Coords'.self::getAddress(), 1);
		}
	}
	
	public function getCoords()
	{
		self::loadData();
		if(is_object(self::getLocationData()))
		{
			if($this->LocationData->results[0]->geometry->location->lat)
			{ self::setLatitude($this->LocationData->results[0]->geometry->location->lat); }
			if($this->LocationData->results[0]->geometry->location->lng)
			{ self::setLongitude($this->LocationData->results[0]->geometry->location->lng); }
			if($this->LocationData->results[0]->status)
			{ self::setStatus($this->LocationData->results[0]->status); }
		}
		if(self::getLatitude() && self::getLongitude())
		{ return self::getLatitude().', '.self::getLongitude(); }
	}
}
?>